

local Widget = require 'widgets/widget' -- 引入基类
local Image = require 'widgets/image'
local Text = require 'widgets/text'
local ImageButton = require 'widgets/imagebutton'

---@class widget_rt_panel : widget_widget
---@field root widget_widget
---@field btn1 widget_imagebutton
---@field btn2 widget_imagebutton
---@field btn3 widget_imagebutton
---@field btn4 widget_imagebutton
---@field btn5 widget_imagebutton
---@field btn6 widget_imagebutton
---@field btn7 widget_imagebutton
---@field btn8 widget_imagebutton
---@field hint widget_text
local rt_panel = Class(Widget, 
---comment
---@param self widget_rt_panel
function(self)
    Widget._ctor(self, 'rt_panel') -- 调用基类构造函数

    self.root = self:AddChild(Widget('root'))
    self.root:SetVAnchor(ANCHOR_MIDDLE)
    self.root:SetHAnchor(ANCHOR_MIDDLE)

    -- right-side
    self.btn1 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_1.tex','hover_1.tex','normal_1.tex','hover_1.tex','normal_1.tex'))
    local x,y = 50,250
    local scale = 0.7
    local x_offset,y_offset = 130,-110
    self.btn1:SetPosition(x,y)
    self.btn1.image:SetScale(scale,scale)

    self.btn2 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_2.tex','hover_2.tex','normal_2.tex','hover_2.tex','normal_2.tex'))
    self.btn2:SetPosition(x+x_offset,y+y_offset)
    self.btn2.image:SetScale(scale,scale)

    local fix_y = -30
    self.btn3 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_2.tex','hover_2.tex','normal_2.tex','hover_2.tex','normal_2.tex'))
    self.btn3:SetPosition(x+x_offset,-(y+y_offset+fix_y))
    self.btn3.image:SetScale(scale,-scale)

    self.btn4 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_1.tex','hover_1.tex','normal_1.tex','hover_1.tex','normal_1.tex'))
    self.btn4:SetPosition(x,-(y+fix_y))
    self.btn4.image:SetScale(scale,-scale)

    -- left-side
    local lx_start = x+x_offset+14
    self.btn8 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_1.tex','hover_1.tex','normal_1.tex','hover_1.tex','normal_1.tex'))
    self.btn8:SetPosition(-lx_start,y)
    self.btn8.image:SetScale(-scale,scale)

    self.btn7 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_2.tex','hover_2.tex','normal_2.tex','hover_2.tex','normal_2.tex'))
    self.btn7.image:SetScale(-scale,scale)
    self.btn7:SetPosition(-(lx_start+x_offset),y+y_offset)

    self.btn6 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_2.tex','hover_2.tex','normal_2.tex','hover_2.tex','normal_2.tex'))
    self.btn6:SetPosition(-(lx_start+x_offset),-(y+y_offset+fix_y))
    self.btn6.image:SetScale(-scale,-scale)

    self.btn5 = self.root:AddChild(ImageButton('images/ui/webbert_rt_btns.xml','normal_1.tex','hover_1.tex','normal_1.tex','hover_1.tex','normal_1.tex'))
    self.btn5:SetPosition(-lx_start,-(y+fix_y))
    self.btn5.image:SetScale(-scale,-scale)

    -- hitn
    self.hint = self.root:AddChild(Text(BODYTEXTFONT, 40, ''))
    self.hint:SetRegionSize(500,500)
    self.hint:SetHAlign(ANCHOR_LEFT)
    self.hint:SetPosition(x+x_offset+400,y/2)


    for i = 1,8 do
        self['btn'..i]:SetText('')
        self['btn'..i]:SetFont(BODYTEXTFONT)
        self['btn'..i]:SetTextSize(35)
        self['btn'..i]:SetTextColour(1,1,1,1)

        self['btn'..i].textfocuscolour = {1,1,1,1}

        self['btn'..i].image:SetFadeAlpha(.8)

        self['btn'..i]:SetOnLoseFocus(function()
            self.hint:SetString('')
        end)
    end

    -- 按钮文字微调
    local text_x,text_y = -20,10
    self.btn1.text:SetPosition(text_x,text_y)
    self.btn4.text:SetPosition(text_x,-text_y)
    self.btn8.text:SetPosition(-text_x,text_y)
    self.btn5.text:SetPosition(-text_x,-text_y)

    text_x,text_y = 0,-25
    self.btn2.text:SetPosition(text_x,text_y)
    self.btn3.text:SetPosition(text_x,-text_y)
    self.btn6.text:SetPosition(-text_x,-text_y)
    self.btn7.text:SetPosition(-text_x,text_y)

end)

---comment
---@param text string
---@param number integer
---@param fn function|nil
---@param hint string|nil
function rt_panel:SetBtn(text,number,fn,hint)
    self['btn'..number]:SetText(text)
    if fn then
        self['btn'..number]:SetOnClick(fn)
    end
    self['btn'..number]:SetOnGainFocus(function()
        if ThePlayer then
            ThePlayer:DoTaskInTime(0,function()
                self.hint:SetString(hint or '')
            end)
        end
    end)
end

function rt_panel:ResetBtns()
    for i = 1,8 do
        self['btn'..i]:SetText('')
        self['btn'..i]:SetFont(BODYTEXTFONT)
        self['btn'..i]:SetTextSize(35)
        self['btn'..i]:SetTextColour(1,1,1,1)

        self['btn'..i].image:SetFadeAlpha(.8)
        self['btn'..i]:SetOnClick(nil)
    end
end


return rt_panel